package tuling;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.DeclareParents;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import tuling.Introductions.ProgramCalculate;
import tuling.Introductions.SimpleProgramCalculate;

import java.util.Arrays;

/**
 * Created by xsls on 2019/6/10.
 */
@Aspect
@Order(1)
@Component
public class TulingLogAspect {

    /*引入:*/
    @DeclareParents(value="tuling.TulingCalculate",   // 动态实现的类
            defaultImpl = SimpleProgramCalculate.class)  // 引入的接口的默认实现
    public static ProgramCalculate programCalculate;    // 引入的接口

    @Pointcut("execution(* tuling.TulingCalculate.*(..))")
    public void pointCut(){};

    @Before(value = "pointCut()")
    public void methodBefore(JoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("11执行目标方法【"+methodName+"】的<前置通知>,入参"+ Arrays.asList(joinPoint.getArgs()));
    }

    @After(value = "pointCut()")
    public void methodAfter(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("11执行目标方法【"+methodName+"】的<后置通知>,入参"+Arrays.asList(joinPoint.getArgs()));
    }


    @AfterReturning(value = "pointCut()",returning = "result")
	public void methodReturning11(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("11执行目标方法11【"+methodName+"】的<返回通知>,入参"+Arrays.asList(joinPoint.getArgs()));
    }





	@AfterThrowing(value = "pointCut()")
    public void methodAfterThrowing(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        System.out.println("11执行目标方法11【"+methodName+"】的<异常通知>,入参"+Arrays.asList(joinPoint.getArgs()));
    }



	@Around(value = "pointCut()")
	public Object logMethodExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("11执行目标方 around start");
		long startTime = System.currentTimeMillis();

		// 执行原始方法
		Object result = joinPoint.proceed();

		long endTime = System.currentTimeMillis();
		long executionTime = endTime - startTime;
		System.out.println("11执行目标方 around end");

		// System.out.println(joinPoint.getSignature() + " ExecutionTime执行时间：" + executionTime + "毫秒");

		return result;
	}


}
